<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Shell命令 | lilei</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/images/icons/logo.png">
    <link rel="apple-touch-icon" href="/images/icons/logo.png">
    <link rel="manifest" href="/manifest.json">
    <script type="text/javascript" src="/assets/js/load.js"></script>
    <meta name="description" content="个人网站">
    <meta name="twitter:title" content="Shell命令">
    <meta name="twitter:description" content="">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:url" content="http://www.lileiblog.xyz/Shell/Shell%E5%91%BD%E4%BB%A4.html">
    <meta property="og:type" content="article">
    <meta property="og:title" content="Shell命令">
    <meta property="og:description" content="">
    <meta property="og:url" content="http://www.lileiblog.xyz/Shell/Shell%E5%91%BD%E4%BB%A4.html">
    <meta property="og:site_name" content="lilei">
    <meta itemprop="name" content="Shell命令">
    <meta itemprop="description" content="">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/images/icons/logo.png">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="keywords" content="lilei,vue,vuepress,vuepress-theme,theme,主题,vuepress主题,blog
        ,vuepress-blog,java,python,shell,sql,golang,script,shell script,nginx,windows,去广告,androd,google drive
        ,one drive,idea,eclipse,git,小程序">
    <meta name="referrer" content="never">
    
    <link rel="preload" href="/assets/css/0.styles.8932179b.css" as="style"><link rel="preload" href="/assets/js/app.269009d6.js" as="script"><link rel="preload" href="/assets/js/3.974b9720.js" as="script"><link rel="preload" href="/assets/js/60.bedb5381.js" as="script"><link rel="prefetch" href="/assets/js/10.5915e99b.js"><link rel="prefetch" href="/assets/js/11.ff732762.js"><link rel="prefetch" href="/assets/js/12.8bb86d91.js"><link rel="prefetch" href="/assets/js/13.18bde5b0.js"><link rel="prefetch" href="/assets/js/14.314fae34.js"><link rel="prefetch" href="/assets/js/15.0a4ff48f.js"><link rel="prefetch" href="/assets/js/16.ffaf1503.js"><link rel="prefetch" href="/assets/js/17.49a31a6e.js"><link rel="prefetch" href="/assets/js/18.7e6443bf.js"><link rel="prefetch" href="/assets/js/19.6880cc9a.js"><link rel="prefetch" href="/assets/js/20.1fb3f255.js"><link rel="prefetch" href="/assets/js/21.0cd39eea.js"><link rel="prefetch" href="/assets/js/22.392d85cb.js"><link rel="prefetch" href="/assets/js/23.f50adb15.js"><link rel="prefetch" href="/assets/js/24.43e324e1.js"><link rel="prefetch" href="/assets/js/25.e3663205.js"><link rel="prefetch" href="/assets/js/26.da141fb8.js"><link rel="prefetch" href="/assets/js/27.6c05235a.js"><link rel="prefetch" href="/assets/js/28.c124245a.js"><link rel="prefetch" href="/assets/js/29.d185870d.js"><link rel="prefetch" href="/assets/js/30.8587d422.js"><link rel="prefetch" href="/assets/js/31.dc382b56.js"><link rel="prefetch" href="/assets/js/32.20ba32e9.js"><link rel="prefetch" href="/assets/js/33.6ecd7de9.js"><link rel="prefetch" href="/assets/js/34.2b91c13a.js"><link rel="prefetch" href="/assets/js/35.66ed0733.js"><link rel="prefetch" href="/assets/js/36.ce7bca84.js"><link rel="prefetch" href="/assets/js/37.d00bb5ac.js"><link rel="prefetch" href="/assets/js/38.c3ecbb6f.js"><link rel="prefetch" href="/assets/js/39.308f4175.js"><link rel="prefetch" href="/assets/js/4.520f6ecc.js"><link rel="prefetch" href="/assets/js/40.f4b98bc2.js"><link rel="prefetch" href="/assets/js/41.95fae64f.js"><link rel="prefetch" href="/assets/js/42.fe4f877b.js"><link rel="prefetch" href="/assets/js/43.ca8f222e.js"><link rel="prefetch" href="/assets/js/44.4d4ee79e.js"><link rel="prefetch" href="/assets/js/45.79178b33.js"><link rel="prefetch" href="/assets/js/46.c3817d0b.js"><link rel="prefetch" href="/assets/js/47.b8252443.js"><link rel="prefetch" href="/assets/js/48.df9d5d3b.js"><link rel="prefetch" href="/assets/js/49.d812fdb2.js"><link rel="prefetch" href="/assets/js/5.f1a24a16.js"><link rel="prefetch" href="/assets/js/50.55cfcaa8.js"><link rel="prefetch" href="/assets/js/51.b8608b02.js"><link rel="prefetch" href="/assets/js/52.52c5e00d.js"><link rel="prefetch" href="/assets/js/53.3a9bd5fb.js"><link rel="prefetch" href="/assets/js/54.a4240c13.js"><link rel="prefetch" href="/assets/js/55.01c4b876.js"><link rel="prefetch" href="/assets/js/56.f29549a3.js"><link rel="prefetch" href="/assets/js/57.01438a04.js"><link rel="prefetch" href="/assets/js/58.29bb4626.js"><link rel="prefetch" href="/assets/js/59.e8b89408.js"><link rel="prefetch" href="/assets/js/6.8c478b39.js"><link rel="prefetch" href="/assets/js/61.bdfbe584.js"><link rel="prefetch" href="/assets/js/62.cb825d1a.js"><link rel="prefetch" href="/assets/js/63.a9516572.js"><link rel="prefetch" href="/assets/js/64.aa854ed7.js"><link rel="prefetch" href="/assets/js/65.285f883d.js"><link rel="prefetch" href="/assets/js/66.12849f37.js"><link rel="prefetch" href="/assets/js/67.c18b00e4.js"><link rel="prefetch" href="/assets/js/68.fa59daa9.js"><link rel="prefetch" href="/assets/js/69.e9d5dda4.js"><link rel="prefetch" href="/assets/js/7.cfb0d0ea.js"><link rel="prefetch" href="/assets/js/70.db126cce.js"><link rel="prefetch" href="/assets/js/71.6acb04cd.js"><link rel="prefetch" href="/assets/js/72.7ff0ab11.js"><link rel="prefetch" href="/assets/js/73.9a66da79.js"><link rel="prefetch" href="/assets/js/74.0a38a030.js"><link rel="prefetch" href="/assets/js/75.7b32dbb3.js"><link rel="prefetch" href="/assets/js/8.cb9c08e5.js"><link rel="prefetch" href="/assets/js/9.ad652cfe.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.110a1065.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8932179b.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/images/icons/logo.png" alt="lilei" class="logo"> <span class="site-name can-hide">lilei</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/Form/" class="nav-link">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link router-link-active">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/Form/" class="nav-link">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link router-link-active">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Shell</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Shell/" aria-current="page" class="sidebar-link">Shell</a></li><li><a href="/Shell/PowerShell.html" class="sidebar-link">PowerShell</a></li><li><a href="/Shell/ShellScript.html" class="sidebar-link">ShellScript</a></li><li><a href="/Shell/ShellWindows.html" class="sidebar-link">ShellWindows</a></li><li><a href="/Shell/Shell命令.html" class="active sidebar-link">Shell命令</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#flag" class="sidebar-link">Flag</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#查看邮箱" class="sidebar-link">查看邮箱</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#清除屏幕输出" class="sidebar-link">清除屏幕输出</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#过滤结果" class="sidebar-link">过滤结果</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#定时任务" class="sidebar-link">定时任务</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#进制转换" class="sidebar-link">进制转换</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#进程与线程" class="sidebar-link">进程与线程</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#延时" class="sidebar-link">延时</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#查找大文件" class="sidebar-link">查找大文件</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#列出目录" class="sidebar-link">列出目录</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#文件查找" class="sidebar-link">文件查找</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#批量替换文件内容" class="sidebar-link">批量替换文件内容</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#删除文件" class="sidebar-link">删除文件</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#列出文件" class="sidebar-link">列出文件</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#显示目录大小" class="sidebar-link">显示目录大小</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#查看当前路径" class="sidebar-link">查看当前路径</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#修改权限" class="sidebar-link">修改权限</a></li><li class="sidebar-sub-header"><a href="/Shell/Shell命令.html#切换目录" class="sidebar-link">切换目录</a></li></ul></li><li><a href="/Shell/WindowsBatch.html" class="sidebar-link">WindowsBatch</a></li><li><a href="/Shell/WindowsJScript.html" class="sidebar-link">WindowsJScript</a></li><li><a href="/Shell/WindowsScript.html" class="sidebar-link">WindowsScript</a></li><li><a href="/Shell/WindowsVBScript.html" class="sidebar-link">WindowsVBScript</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="shell命令"><a href="#shell命令" class="header-anchor">#</a> Shell命令</h1> <p></p><div class="table-of-contents"><ul><li><a href="#flag">Flag</a></li><li><a href="#查看邮箱">查看邮箱</a></li><li><a href="#清除屏幕输出">清除屏幕输出</a></li><li><a href="#过滤结果">过滤结果</a><ul><li><a href="#截取末尾字符串">截取末尾字符串</a></li><li><a href="#过滤多个结果">过滤多个结果</a></li></ul></li><li><a href="#定时任务">定时任务</a><ul><li><a href="#编辑crontab文件">编辑crontab文件</a></li><li><a href="#输入定时任务命令">输入定时任务命令</a></li></ul></li><li><a href="#进制转换">进制转换</a></li><li><a href="#进程与线程">进程与线程</a></li><li><a href="#延时">延时</a></li><li><a href="#查找大文件">查找大文件</a></li><li><a href="#列出目录">列出目录</a></li><li><a href="#文件查找">文件查找</a></li><li><a href="#批量替换文件内容">批量替换文件内容</a></li><li><a href="#删除文件">删除文件</a></li><li><a href="#列出文件">列出文件</a></li><li><a href="#显示目录大小">显示目录大小</a></li><li><a href="#查看当前路径">查看当前路径</a></li><li><a href="#修改权限">修改权限</a></li><li><a href="#切换目录">切换目录</a></li></ul></div><p></p> <h2 id="flag"><a href="#flag" class="header-anchor">#</a> Flag</h2> <blockquote><p><code>Shell</code>是一个用<code>C</code>语言编写的程序，它是用户使用<code>Linux</code>的桥梁。文字操作系统与外部最主要的接口就叫做<code>Shell</code>。<code>Shell</code>是操作系统最外面的一层。</p> <p><code>Shell</code>管理你与操作系统之间的交互：等待你输入，向操作系统解释你的输入，并且处理各种各样的操作系统的输出结果。</p></blockquote> <ul><li><a href="https://github.com/awesome-lists/awesome-bash" target="_blank" rel="noopener noreferrer">https://github.com/awesome-lists/awesome-bash<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li><a href="https://blog.csdn.net/Mculover666/article/details/84558280" target="_blank" rel="noopener noreferrer">Linux常用命令汇总<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li><a href="https://www.cnblogs.com/EasonJim/p/8018545.html" target="_blank" rel="noopener noreferrer">Linux下Shell脚本字符串单引号、双引号、反引号、反斜杠的作用和区别<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <h2 id="查看邮箱"><a href="#查看邮箱" class="header-anchor">#</a> 查看邮箱</h2> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">cat</span> /var/spool/mail/root
</code></pre></div><h2 id="清除屏幕输出"><a href="#清除屏幕输出" class="header-anchor">#</a> 清除屏幕输出</h2> <blockquote><p><code>clear</code>命令、<code>reset</code>命令（可修正光标位置）或<kbd>Ctrl</kbd> + <kbd>L</kbd>组合快捷键，并不是真正的清空屏幕，当用鼠标向上滚时，你仍然能看到之前的命令操作留下来的输出</p></blockquote> <blockquote><p><code>printf &quot;\033c&quot;</code>或<code>printf &quot;\ec&quot;</code>真正的清空了终端屏幕，它的功能跟<code>DOS</code>里<code>CMD.EXE</code>提供的<code>CLS</code>效果很相似。
<code>\033 == \x1B == 27 == ESC</code>，于是，这个命令变成了<code>&lt;ESC&gt;c</code>，它是VT-XXX中表示<code>Full Reset (RIS)</code>的转义码。
现今我们使用的所有的终端都是VT兼容的，但如果你发现自己使用的是一个非常奇怪的终端，那这个命令你可能用不了。<code>printf</code>是<code>bash</code>里内置的命令，内置命令的优先级比其它可执行文件要高。</p></blockquote> <h2 id="过滤结果"><a href="#过滤结果" class="header-anchor">#</a> 过滤结果</h2> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">ls</span> -l <span class="token operator">|</span> <span class="token function">grep</span> <span class="token builtin class-name">test</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{print $5}'</span> <span class="token operator">|</span> <span class="token function">sed</span> -n <span class="token string">'2p'</span>
</code></pre></div><ul><li><code>grep</code> 文本过滤命令，包含test的文件</li></ul> <blockquote><p>由正则表达式或者字符及基本文本字符所编写的过滤条件</p></blockquote> <table><thead><tr><th>参数</th> <th>说明</th></tr></thead> <tbody><tr><td>-a --text</td> <td>不要忽略二进制数据。</td></tr> <tr><td>-A &lt;显示行数&gt;   --after-context=&lt;显示行数&gt;</td> <td>除了显示符合范本样式的那一行之外，并显示该行之后的内容。</td></tr> <tr><td>-b --byte-offset</td> <td>在显示符合范本样式的那一行之外，并显示该行之前的内容。</td></tr> <tr><td>-B&lt;显示行数&gt;   --before-context=&lt;显示行数&gt;</td> <td>除了显示符合样式的那一行之外，并显示该行之前的内容。</td></tr> <tr><td>-c --count</td> <td>计算符合范本样式的列数。</td></tr> <tr><td>-C&lt;显示行数&gt; --context=&lt;显示行数&gt;或-&lt;显示行数&gt;</td> <td>除了显示符合范本样式的那一列之外，并显示该列之前后的内容。</td></tr> <tr><td>-d&lt;进行动作&gt; --directories=&lt;动作&gt;</td> <td>当指定要查找的是目录而非文件时，必须使用这项参数，否则grep命令将回报信息并停止动作。</td></tr> <tr><td>-e&lt;范本样式&gt; --regexp=&lt;范本样式&gt;</td> <td>指定字符串作为查找文件内容的范本样式。</td></tr> <tr><td>-E --extended-regexp</td> <td>将范本样式为延伸的普通表示法来使用，意味着使用能使用扩展正则表达式。</td></tr> <tr><td>-f&lt;范本文件&gt; --file=&lt;规则文件&gt;</td> <td>指定范本文件，其内容有一个或多个范本样式，让grep查找符合范本条件的文件内容，格式为每一列的范本样式。</td></tr> <tr><td>-F --fixed-regexp</td> <td>将范本样式视为固定字符串的列表。</td></tr> <tr><td>-G --basic-regexp</td> <td>将范本样式视为普通的表示法来使用。</td></tr> <tr><td>-h --no-filename</td> <td>在显示符合范本样式的那一列之前，不标示该列所属的文件名称。</td></tr> <tr><td>-H --with-filename</td> <td>在显示符合范本样式的那一列之前，标示该列的文件名称。</td></tr> <tr><td>-i --ignore-case</td> <td>忽略字符大小写的差别。</td></tr> <tr><td>-l --file-with-matches</td> <td>列出文件内容符合指定的范本样式的文件名称。</td></tr> <tr><td>-L --files-without-match</td> <td>列出文件内容不符合指定的范本样式的文件名称。</td></tr> <tr><td>-n --line-number</td> <td>在显示符合范本样式的那一列之前，标示出该列的编号。</td></tr> <tr><td>-P --perl-regexp</td> <td>PATTERN 是一个 Perl 正则表达式</td></tr> <tr><td>-q --quiet或--silent</td> <td>不显示任何信息。</td></tr> <tr><td>-R/-r  --recursive</td> <td>此参数的效果和指定“-d recurse”参数相同。</td></tr> <tr><td>-s --no-messages</td> <td>不显示错误信息。</td></tr> <tr><td>-v --revert-match</td> <td>反转查找。</td></tr> <tr><td>-V --version</td> <td>显示版本信息。</td></tr> <tr><td>-w --word-regexp</td> <td>只显示全字符合的列。</td></tr> <tr><td>-x --line-regexp</td> <td>只显示全列符合的列。</td></tr> <tr><td>-y</td> <td>此参数效果跟“-i”相同。</td></tr> <tr><td>-o</td> <td>只输出文件中匹配到的部分。</td></tr> <tr><td>-m 数量 --max-count=数量</td> <td>找到num行结果后停止查找，用来限制匹配行数</td></tr></tbody></table> <ul><li><p><code>awk</code> 文本处理命令，<code>print</code>后面是你要获取第几列</p></li> <li><p><code>sed</code> 行编辑器，<code>-n</code>是指定第几行。</p> <ul><li><code>p</code> 显示</li> <li><code>d</code> 删除</li> <li><code>a</code> 添加</li> <li><code>c</code> 替换</li> <li><code>w</code> 写入</li> <li><code>i</code> 插入</li></ul></li> <li><p><code>cut</code>命令用于显示每行从开头算起num1到num2的文字</p> <ul><li><code>-b</code> ：以字节为单位进行分割。这些字节位置将忽略多字节字符边界，除非也指定了<code>-n</code>标志。</li> <li><code>-c</code> ：以字符为单位进行分割。</li> <li><code>-d</code> ：自定义分隔符，默认为制表符。</li> <li><code>-f</code> ：与-d一起使用，指定显示哪个区域。</li> <li><code>-n</code> ：取消分割多字节字符。仅和 -b 标志一起使用。</li> <li>如果字符的最后一个字节落在由<code>-b</code>标志的List参数指示的范围之内，该字符将被写出；否则，该字符将被排除</li></ul></li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code>ll <span class="token operator">|</span> <span class="token function">cut</span> -c1-10
</code></pre></div><h3 id="截取末尾字符串"><a href="#截取末尾字符串" class="header-anchor">#</a> 截取末尾字符串</h3> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token builtin class-name">echo</span> <span class="token string">&quot;1:3:5&quot;</span> <span class="token operator">|</span> <span class="token function">awk</span> -F <span class="token string">&quot;:&quot;</span> <span class="token string">'{print $NF}'</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;1:3:5&quot;</span> <span class="token operator">|</span> <span class="token function">sed</span> <span class="token string">'s/.*:\([^:]*\)$/\1/'</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;1:3:5&quot;</span> <span class="token operator">|</span> <span class="token function">cut</span> -d <span class="token builtin class-name">:</span> -f <span class="token number">3</span>
</code></pre></div><h3 id="过滤多个结果"><a href="#过滤多个结果" class="header-anchor">#</a> 过滤多个结果</h3> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">ls</span> -l <span class="token operator">|</span> <span class="token function">grep</span> <span class="token string">&quot;postfix\|dovecot&quot;</span>
<span class="token comment"># 或者</span>
<span class="token function">ls</span> -l <span class="token operator">|</span> <span class="token function">grep</span> -e postfix -e dovecot
<span class="token comment"># 或者</span>
<span class="token function">ls</span> -l <span class="token operator">|</span> <span class="token function">grep</span> -E <span class="token string">&quot;postfix|dovecot&quot;</span>
<span class="token comment"># 或者</span>
<span class="token function">ls</span> -l <span class="token operator">|</span> <span class="token function">egrep</span> <span class="token string">&quot;postfix|dovecot&quot;</span>
<span class="token comment"># 或者</span>
<span class="token function">ls</span> -l <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">&quot;/postfix|dovecot/&quot;</span>
</code></pre></div><h2 id="定时任务"><a href="#定时任务" class="header-anchor">#</a> 定时任务</h2> <blockquote><p><code>crontab</code>命令常用于Unix和类Unix的操作系统之中，用于设置周期性被执行的指令</p></blockquote> <h3 id="编辑crontab文件"><a href="#编辑crontab文件" class="header-anchor">#</a> 编辑crontab文件</h3> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">crontab</span> -e
</code></pre></div><h3 id="输入定时任务命令"><a href="#输入定时任务命令" class="header-anchor">#</a> 输入定时任务命令</h3> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># 每分钟输出一次当前时间</span>
* * * * * <span class="token builtin class-name">echo</span> <span class="token variable"><span class="token variable">`</span><span class="token function">date</span><span class="token variable">`</span></span> <span class="token operator">&gt;&gt;</span> /log.log
<span class="token comment"># 每天凌晨1点30分执行清理内存脚本，并且输出到日志</span>
<span class="token number">30</span> <span class="token number">1</span> * * *  /bin/bash /home/rememory.sh <span class="token operator">&gt;&gt;</span> /home/rememory.log <span class="token operator"><span class="token file-descriptor important">2</span>&gt;</span><span class="token file-descriptor important">&amp;1</span>
<span class="token comment"># 每天凌晨1点30分执行删除MySQL日志文件，并且输出到日志</span>
<span class="token number">30</span> <span class="token number">1</span> * * *  python /home/delete_file.py 文件带后缀的路径 保留的文件个数 <span class="token operator">&gt;&gt;</span> /home/delete_file.log <span class="token operator"><span class="token file-descriptor important">2</span>&gt;</span><span class="token file-descriptor important">&amp;1</span>
<span class="token comment"># 每隔3天,1点30分执行，并且输出到日志</span>
<span class="token number">30</span> <span class="token number">1</span> */3 * * /bin/bash 文件路径 <span class="token operator">&gt;&gt;</span> 输出日志文件路径 <span class="token operator"><span class="token file-descriptor important">2</span>&gt;</span><span class="token file-descriptor important">&amp;1</span>
<span class="token comment"># 设置每20天清理一次（日志清理太频繁不方便以后按日志排错）</span>
<span class="token number">0</span> <span class="token number">0</span> */20 * * /bin/bash /home/cleanLog.sh <span class="token operator">&gt;&gt;</span> /home/cleanLog.log <span class="token operator"><span class="token file-descriptor important">2</span>&gt;</span><span class="token file-descriptor important">&amp;1</span>

<span class="token comment"># 设置每小时执行一次</span>
<span class="token number">0</span> */1 * * *  执行命令
</code></pre></div><blockquote><p>在linux中的直接执行shell脚本可以用相对路径找到文件,但是如果通过计划任务<code>crontab</code>执行shell脚本时，却不能通过相对路径找到文件!</p> <p>可以使用pwd命令获取目录<code>pwd</code>'/文件名'</p></blockquote> <h2 id="进制转换"><a href="#进制转换" class="header-anchor">#</a> 进制转换</h2> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># 转换为16进制,大写转换为小写。</span>
<span class="token builtin class-name">echo</span> <span class="token string">&quot;obase=16; 值&quot;</span> <span class="token operator">|</span> <span class="token function">bc</span>
<span class="token comment"># 转换为16进制，\n换行</span>
<span class="token builtin class-name">printf</span> <span class="token string">&quot;%x<span class="token entity" title="\n">\n</span>&quot;</span> 值
</code></pre></div><h2 id="进程与线程"><a href="#进程与线程" class="header-anchor">#</a> 进程与线程</h2> <p><strong>查看进程启动路径</strong></p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">ps</span> -aux <span class="token operator">|</span> <span class="token function">grep</span> nginx
<span class="token comment"># 每个进程启动之后在 /proc下面有一个于pid对应的路径</span>
<span class="token function">ls</span> -l /proc/PID
</code></pre></div><p><strong>查看系统进程和线程限制</strong></p> <ul><li>查看系统允许的最大线程数</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token builtin class-name">ulimit</span> -a <span class="token operator">|</span> <span class="token function">grep</span> processes
<span class="token builtin class-name">ulimit</span> -u
</code></pre></div><ul><li>系统支持的最大线程数</li></ul> <blockquote><p>操作系统线程ID的最大值</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">cat</span> /proc/sys/kernel/pid_max
</code></pre></div><ul><li>内核支持的最大线程数</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code>sysctl kernel.pid_max
</code></pre></div><blockquote><p>修改值（临时）：<code>sysctl -w kernel.pid_max=值</code></p></blockquote> <blockquote><p>修改值（永久）：<code>echo 'kernel.pid_max=值' &gt;&gt;/etc/sysctl.conf</code></p></blockquote> <ul><li>单个进程可创建的线程数</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">cat</span> /proc/sys/vm/max_map_count
</code></pre></div><p><strong>查看进程的信息</strong></p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">cat</span> /proc/进程PID/status
</code></pre></div><p><strong>查看线程树</strong></p> <blockquote><p>如果不跟进程PID就查看系统中所有的进行线程树</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code>pstree -p 进程PID
</code></pre></div><p><strong>查看进程中的线程</strong></p> <blockquote><p><code>top -p 进程PID</code> 再按<code>Shift+h</code>（也就是大写<code>H</code>）</p></blockquote> <blockquote><p>或者直接输入<code>top -bH -d 3 -p 进程PID</code></p></blockquote> <blockquote><p><code>top -H</code> 加上<code>-H</code>这个选项启动<code>top</code>，一行显示一个线程。否则，它一行显示一个进程。</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">top</span> -H -p 进程PID
<span class="token function">top</span> -Hp 进程PID
</code></pre></div><p><strong>查看线程列表</strong></p> <blockquote><p>查看所有存在的线程</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">ps</span> xH
</code></pre></div><p><strong>查看线程数量</strong></p> <div class="language-bash extra-class"><pre class="language-bash"><code>pstree -p <span class="token operator">|</span> <span class="token function">wc</span> -l

pstree -p 进程PID <span class="token operator">|</span> <span class="token function">wc</span> -l

<span class="token function">ps</span> -hH -p 进程PID <span class="token operator">|</span> <span class="token function">wc</span> -l

<span class="token function">ps</span> -mp 进程PID -o THREAD,tid,time <span class="token operator">|</span> <span class="token function">wc</span> -l

<span class="token function">ls</span> /proc/进程PID/task <span class="token operator">|</span> <span class="token function">wc</span> -l
</code></pre></div><p><strong>查看进程下的线程状态</strong></p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">ps</span> -mp 进程PID -o THREAD,tid,time <span class="token operator">|</span> <span class="token function">sort</span> -rn
</code></pre></div><h2 id="延时"><a href="#延时" class="header-anchor">#</a> 延时</h2> <ul><li><code>sleep</code> 默认以秒为单位</li></ul> <blockquote><p><code>sleep 1</code> 睡眠1秒</p> <p><code>sleep 1s</code> 表示延迟一秒</p> <p><code>sleep 1m</code> 表示延迟一分钟</p> <p><code>sleep 1h</code> 表示延迟一小时</p> <p><code>sleep 1d</code> 表示延迟一天</p></blockquote> <ul><li><code>usleep</code> 默认以微秒为单位</li></ul> <blockquote><p>1s = 1000ms = 1000000us</p></blockquote> <h2 id="查找大文件"><a href="#查找大文件" class="header-anchor">#</a> 查找大文件</h2> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># 查找从根目录下查找大于100M的文件，并显示文件的具体大小再进行排序</span>
<span class="token function">find</span> / -type f -size +100M -print0 <span class="token operator">|</span> <span class="token function">xargs</span> -0 <span class="token function">du</span> -h <span class="token operator">|</span> <span class="token function">sort</span> -nr

<span class="token comment"># 查找从根目录下查找大于1G的文件，并显示文件的具体大小再进行排序</span>
<span class="token function">find</span> / -type f -size +1G -print0 <span class="token operator">|</span> <span class="token function">xargs</span> -0 <span class="token function">du</span> -h <span class="token operator">|</span> <span class="token function">sort</span> -nr

<span class="token comment"># 有时候排列的顺序并不完全是按大小一致，这个是因为du命令的参数h所致，可以使用MB来显示</span>
<span class="token function">find</span> / -type f -size +100M -print0 <span class="token operator">|</span> <span class="token function">xargs</span> -0 <span class="token function">du</span> -hm <span class="token operator">|</span> <span class="token function">sort</span> -nr

<span class="token comment"># 查找从根目录下查找大于100M的文件，并显示文件的属性信息</span>
<span class="token function">find</span> / -type f -size +100M -print0 <span class="token operator">|</span> <span class="token function">xargs</span> -0 <span class="token function">ls</span> -l

<span class="token comment"># 查看当前文件夹下的文件夹占用大小并排序</span>
<span class="token function">du</span> -h --max-depth<span class="token operator">=</span><span class="token number">2</span> <span class="token operator">|</span> <span class="token function">sort</span> -n

<span class="token comment"># 有时候排列的顺序并不完全是按大小一致，这个是因为du命令的参数h所致，可以使用MB来显示</span>
<span class="token function">du</span> -hm --max-depth<span class="token operator">=</span><span class="token number">2</span> <span class="token operator">|</span> <span class="token function">sort</span> -n

<span class="token comment"># 找出当前文件夹下最大的12个文件夹</span>
<span class="token function">du</span> -hm --max-depth<span class="token operator">=</span><span class="token number">2</span> <span class="token operator">|</span> <span class="token function">sort</span> -nr <span class="token operator">|</span> <span class="token function">head</span> -12

<span class="token comment"># 查看home文件夹下的所有文件夹占用大小并排序</span>
<span class="token function">du</span> -hm --max-depth<span class="token operator">=</span><span class="token number">2</span> /home/ <span class="token operator">|</span> <span class="token function">sort</span> -nr

<span class="token comment"># 查找目录</span>
<span class="token function">find</span> / -name <span class="token string">'path'</span> -type d

<span class="token comment"># 查找内容</span>
<span class="token function">find</span> <span class="token builtin class-name">.</span> <span class="token operator">|</span> <span class="token function">xargs</span> <span class="token function">grep</span> -ri <span class="token string">'content'</span>
<span class="token comment"># 查找内容只显示文件名称</span>
<span class="token function">find</span> <span class="token builtin class-name">.</span> <span class="token operator">|</span> <span class="token function">xargs</span> <span class="token function">grep</span> -ril <span class="token string">'content'</span>
</code></pre></div><h2 id="列出目录"><a href="#列出目录" class="header-anchor">#</a> 列出目录</h2> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">ls</span> -l <span class="token operator">|</span> <span class="token function">grep</span> <span class="token string">&quot;^d&quot;</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{print $9}'</span>
<span class="token function">ls</span> -F <span class="token operator">|</span> <span class="token function">grep</span> <span class="token string">&quot;/$&quot;</span>
<span class="token function">ls</span> <span class="token operator">|</span> <span class="token function">sed</span> <span class="token string">&quot;s:^:<span class="token variable"><span class="token variable">`</span><span class="token builtin class-name">pwd</span><span class="token variable">`</span></span>/: &quot;</span>
<span class="token function">ls</span> -1 <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{print i$0}'</span> <span class="token assign-left variable">i</span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token builtin class-name">pwd</span><span class="token variable">`</span></span><span class="token string">'/'</span>
<span class="token function">ls</span> -d */
<span class="token function">ls</span> -ad */
</code></pre></div><h2 id="文件查找"><a href="#文件查找" class="header-anchor">#</a> 文件查找</h2> <blockquote><p><code>find</code>用于查找文件</p></blockquote> <ul><li><code>grep</code>用于查找文件内容的行
<ul><li><code>-r</code>递归查找</li> <li><code>-l</code>列出匹配的文件名</li> <li><code>-n</code>列出所在的行号</li> <li><code>--include=&quot;*.text&quot;</code>只查找后缀名是<code>.text</code>的文件</li> <li><code>--exclude=&quot;*.sql&quot;</code>查找除了后缀名是<code>.sql</code>的文件</li></ul></li></ul> <blockquote><p><code>xargs</code>命令是给其他命令传递参数的一个过滤器，也是组合多个命令的一个工具，它擅成长将标准输入数据转换成命令行参数。</p></blockquote> <blockquote><p>使用<code>find</code>命令的<code>-exec</code>选项处理匹配到的文件时，<code>find</code>命令将所有匹配到的文件一起传递给<code>exec</code>。
不幸的是，有些系统对能够传递给<code>exec</code>的命令长度有限制，这样<code>find</code>命令运行几分钟之后就算出现溢出错误。</p> <blockquote><p>错误信息通常是&quot;参数列太长&quot;或&quot;参数列溢出&quot;。</p></blockquote> <p>这就是<code>xargs</code>的用处所在，特别是与<code>find</code>命令一起使用,<code>exec</code>会发起多个进程，而<code>xargs</code>只有一个</p></blockquote> <blockquote><p>查看某个文件，注意权限问题</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">find</span> -name <span class="token builtin class-name">test</span>
<span class="token comment"># 查找并排除多个文件</span>
<span class="token function">find</span> ./ -not <span class="token punctuation">\</span><span class="token punctuation">(</span> -name <span class="token string">&quot;*.jpg&quot;</span> -o -name <span class="token string">&quot;*.png&quot;</span> <span class="token punctuation">\</span><span class="token punctuation">)</span>
<span class="token function">find</span> ./ -not -name <span class="token string">&quot;*.jpg&quot;</span> -o -name <span class="token string">&quot;*.png&quot;</span>
<span class="token function">find</span> ./ -not <span class="token punctuation">\</span><span class="token punctuation">(</span> -name <span class="token string">&quot;*.jpg&quot;</span> -or -name <span class="token string">&quot;*.png&quot;</span> <span class="token punctuation">\</span><span class="token punctuation">)</span>
<span class="token function">find</span> ./ -not -name <span class="token string">&quot;*.jpg&quot;</span> -or -name <span class="token string">&quot;*.png&quot;</span>
<span class="token comment"># 查看录前目录下文件名中含有字符串的文件，`*`为通配符，可以按需要使用</span>
<span class="token function">find</span> -name <span class="token string">'*XXX*'</span>
<span class="token comment"># 在当前目录下查看所有目录并排序</span>
<span class="token function">find</span> -type d <span class="token operator">|</span> <span class="token function">sort</span>
<span class="token comment"># 查找指定时间内的文件</span>
<span class="token function">find</span> 文件路径 -type f -newermt <span class="token string">'起始时间'</span> -a -not -newermt <span class="token string">'结束时间'</span>
<span class="token comment"># 查找当前目录下文件内容匹配的字符串，输出：`全路径文件名:字符串所在行内容`</span>
<span class="token function">find</span> <span class="token builtin class-name">.</span> -type f <span class="token operator">|</span> <span class="token function">xargs</span> <span class="token function">grep</span> <span class="token string">&quot;XXX&quot;</span>
<span class="token function">find</span> <span class="token builtin class-name">.</span> <span class="token operator">|</span> <span class="token function">xargs</span> <span class="token function">grep</span> -ri <span class="token string">&quot;XXX&quot;</span>
<span class="token comment"># 查找当前目录下文件内容匹配的字符串，输出：`字符串所在行内容`</span>
<span class="token function">find</span> /XXX/XXX -type f -exec <span class="token function">grep</span> <span class="token string">&quot;XXX&quot;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token punctuation">\</span><span class="token punctuation">;</span>
<span class="token comment"># 查找当前目录下文件内容匹配的字符串，输出：`文件名`</span>
<span class="token function">find</span> <span class="token builtin class-name">.</span> <span class="token operator">|</span> <span class="token function">xargs</span> <span class="token function">grep</span> -ril <span class="token string">&quot;XXX&quot;</span>
</code></pre></div><div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># 在指定文件中（一个或多个）查找并出含字符串的行</span>
<span class="token function">grep</span> <span class="token string">'XXX'</span> text1.txt text2.txt
<span class="token comment"># 在以t开头的文件中查找并出含字符串的行</span>
<span class="token function">grep</span> <span class="token string">'XXX'</span> t*
</code></pre></div><h2 id="批量替换文件内容"><a href="#批量替换文件内容" class="header-anchor">#</a> 批量替换文件内容</h2> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">find</span> -name <span class="token string">'要查找的文件名'</span> <span class="token operator">|</span> <span class="token function">xargs</span> perl -pi -e <span class="token string">'s|被替换的字符串|替换后的字符串|g'</span>
</code></pre></div><blockquote><p><code>sed</code>命令可以批量替换多个文件中的字符串</p></blockquote> <blockquote><p>注意命令中的反撇号，而不是单引号</p> <p>反撇号：功能是将命令的输出结果给变量；在这里的作用是将输出查找到的文件名给<code>sed</code>用于字符串替换。</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">sed</span> -i <span class="token string">&quot;s/原字符串/新字符串/g&quot;</span> <span class="token variable"><span class="token variable">`</span><span class="token function">grep</span> 原字符串 -rl 所在目录<span class="token variable">`</span></span>
<span class="token function">grep</span> <span class="token string">&quot;原字符串&quot;</span> -rl 所在目录 <span class="token operator">|</span> <span class="token function">xargs</span> <span class="token function">sed</span> -i <span class="token string">&quot;s/原字符串/新字符串/g&quot;</span>
<span class="token function">grep</span> -rl <span class="token string">&quot;原字符串&quot;</span> ./* <span class="token operator">|</span> <span class="token function">xargs</span> <span class="token function">sed</span> -i s/<span class="token string">&quot;原字符串&quot;</span>/<span class="token string">&quot;新字符串&quot;</span>/g
</code></pre></div><blockquote><p>一次性将所有文件中的指定字符串进行修改</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">grep</span> <span class="token string">&quot;原字符串&quot;</span> * -R <span class="token operator">|</span> <span class="token function">awk</span> -F: <span class="token string">'{print $1}'</span> <span class="token operator">|</span> <span class="token function">sort</span> <span class="token operator">|</span> <span class="token function">uniq</span> <span class="token operator">|</span> <span class="token function">xargs</span> <span class="token function">sed</span> -i <span class="token string">'s/原字符串/新字符串/g'</span>
</code></pre></div><blockquote><p>统计匹配指定字符串的所有sql文件的数量</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">grep</span> -rl <span class="token string">&quot;XXX&quot;</span> --exclude<span class="token operator">=</span><span class="token string">&quot;*.sql&quot;</span> ./* <span class="token operator">|</span> <span class="token function">wc</span> -l
</code></pre></div><h2 id="删除文件"><a href="#删除文件" class="header-anchor">#</a> 删除文件</h2> <p><strong>删除排除的其他文件</strong></p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">find</span> * <span class="token operator">|</span> <span class="token function">grep</span> -v <span class="token string">'\(*.jpg\|*.png)'</span> <span class="token operator">|</span> <span class="token function">xargs</span> <span class="token function">rm</span>
<span class="token function">find</span> <span class="token builtin class-name">.</span> -maxdepth <span class="token number">1</span> <span class="token operator">!</span> -name <span class="token string">'dist'</span> -type f -exec <span class="token function">rm</span> -v <span class="token punctuation">{</span><span class="token punctuation">}</span> +
<span class="token comment">#报错：`-bash: !: event not found` 需要开启通配符功能</span>
<span class="token builtin class-name">shopt</span> -s extglob <span class="token operator">&amp;&amp;</span> <span class="token function">rm</span> -rf <span class="token operator">!</span><span class="token punctuation">(</span>*.jpg<span class="token operator">|</span>*.png<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">shopt</span> -u extglob
<span class="token comment"># rm删除除去指定文件的剩余所有文件 (rm 反向删除）</span>
<span class="token function">rm</span> -rf <span class="token variable"><span class="token variable">`</span><span class="token function">ls</span> <span class="token operator">|</span> <span class="token function">grep</span> -v <span class="token string">&quot;^ab.txt$&quot;</span><span class="token variable">`</span></span>
<span class="token function">rm</span> -rf <span class="token variable"><span class="token variable">`</span><span class="token function">ls</span> <span class="token operator">|</span> <span class="token function">egrep</span> -v <span class="token string">'(config.js|dist)'</span><span class="token variable">`</span></span>
<span class="token comment"># 递归当前目录</span>
<span class="token function">rm</span> -f <span class="token variable"><span class="token variable">`</span><span class="token function">find</span> * -name <span class="token string">&quot;*&quot;</span> <span class="token operator">|</span> <span class="token function">egrep</span> -v <span class="token string">'(*.jpg|*.png)'</span><span class="token variable">`</span></span>
</code></pre></div><p><strong>删除指定时间前的文件</strong></p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># 统计目录下N天前的文件数量</span>
<span class="token function">find</span> 目录 -type f -mtime +天数 <span class="token operator">|</span> <span class="token function">wc</span> -l

<span class="token comment"># 列出目录下N天前的文件</span>
<span class="token function">find</span> 目录 -type f -mtime +天数 -exec <span class="token function">ls</span> -l <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token punctuation">\</span><span class="token punctuation">;</span>

<span class="token comment"># 删除目录下N天前的文件</span>
<span class="token function">find</span> 目录 -type f -mtime +天数 -exec <span class="token function">rm</span> -rf <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token punctuation">\</span><span class="token punctuation">;</span>

<span class="token comment"># 删除目录下N天前的文件</span>
<span class="token function">find</span> 目录 -type f -mtime +天数 <span class="token operator">|</span> <span class="token function">xargs</span> -I <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token function">rm</span> -rf <span class="token punctuation">{</span><span class="token punctuation">}</span>


<span class="token comment"># 删除目录下N天前文件名包含tar的文件或目录</span>
<span class="token function">find</span> 目录 -mtime +天数 -name <span class="token string">&quot;*.tar.*&quot;</span> -exec <span class="token function">rm</span> -rf <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token punctuation">\</span><span class="token punctuation">;</span>

<span class="token comment"># 删除目录下N天前文件名为tar.gz后缀的文件或目录</span>
<span class="token function">find</span> 目录 -mtime +天数 -name <span class="token string">&quot;*.tar.gz&quot;</span> <span class="token operator">|</span> <span class="token function">xargs</span> -I <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token function">rm</span> -rf <span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre></div><p><strong>删除指定大小的文件</strong></p> <blockquote><p><code>-lt 50</code> 删除小于50KB的文件</p> <p><code>-gt 50</code> 删除大于50KB的文件</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token keyword">for</span> <span class="token for-or-select variable">file</span> <span class="token keyword">in</span> <span class="token variable"><span class="token variable">`</span><span class="token function">ls</span> ./<span class="token variable">`</span></span><span class="token punctuation">;</span> <span class="token keyword">do</span> <span class="token assign-left variable">size</span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token function">du</span> $file <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'{print \$1}'</span><span class="token variable">`</span></span><span class="token punctuation">;</span> <span class="token punctuation">[</span> <span class="token variable">$size</span> -lt <span class="token number">50</span> <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> <span class="token function">rm</span> <span class="token variable">$file</span><span class="token punctuation">;</span> <span class="token keyword">done</span>
</code></pre></div><blockquote><p><code>-size 1k</code> 删除1KB占用空间的文件</p> <p><code>-size +100k</code> 删除大于100KB占用空间的文件</p> <p><code>-size -1k</code> 删除小于1KB占用空间的文件</p></blockquote> <blockquote><p><code>-size 1024c</code> 删除1k大小的文件</p> <p><code>-size +1024c</code> 删除大于1k大小的文件</p> <p><code>-size -1024c</code> 删除小于1k大小的文件</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">find</span> 目录 -type f -size 大小 -exec <span class="token function">rm</span> -rf <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token punctuation">\</span><span class="token punctuation">;</span>
<span class="token function">find</span> 目录 -type f -size 大小 <span class="token operator">|</span> <span class="token function">xargs</span> -n <span class="token number">1</span> <span class="token function">rm</span> -rf
</code></pre></div><h2 id="列出文件"><a href="#列出文件" class="header-anchor">#</a> 列出文件</h2> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># 查看所有文件（包括隐藏文件）并以最大容量单位显示</span>
ll -a -h 文件名
</code></pre></div><h2 id="显示目录大小"><a href="#显示目录大小" class="header-anchor">#</a> 显示目录大小</h2> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">du</span> -sh
</code></pre></div><h2 id="查看当前路径"><a href="#查看当前路径" class="header-anchor">#</a> 查看当前路径</h2> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token builtin class-name">pwd</span>
</code></pre></div><h2 id="修改权限"><a href="#修改权限" class="header-anchor">#</a> 修改权限</h2> <blockquote><p>一次性更改权限就使用-R,文件修改为所有用户可读可写可执行，也就是对应编号为777</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">chmod</span> -R <span class="token number">777</span> 文件名
</code></pre></div><blockquote><p>使用命令chown改变目录或文件的所有权,更改所有者和所属组chown(change owner缩写）</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code>chown:用户名 文件名
</code></pre></div><h2 id="切换目录"><a href="#切换目录" class="header-anchor">#</a> 切换目录</h2> <ul><li>进入当前用户的主目录</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token builtin class-name">cd</span> ~
<span class="token builtin class-name">cd</span> 
</code></pre></div><ul><li>进入上次所在目录</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token builtin class-name">cd</span> -
</code></pre></div><ul><li>进入上一级目录</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token builtin class-name">cd</span> <span class="token punctuation">..</span>
</code></pre></div><ul><li>进入当前目录</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token builtin class-name">cd</span> <span class="token builtin class-name">.</span>
</code></pre></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/woytu/notes-vuepress/edit/master//Shell/Shell命令.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2021-05-25 13:44:56</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/Shell/ShellWindows.html" class="prev">
        ShellWindows
      </a></span> <span class="next"><a href="/Shell/WindowsBatch.html">
        WindowsBatch
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.269009d6.js" defer></script><script src="/assets/js/3.974b9720.js" defer></script><script src="/assets/js/60.bedb5381.js" defer></script>
  </body>
</html>
